﻿<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="WireBear.Ribbon.InfoPathPrinter"
                Location="CommandUI.Ribbon"
                Sequence="20">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Templates._children">
          <GroupTemplate Id="WireBear.Ribbon.Templates.Groups.InfoPathPrinter">
            <Layout Title="InfoPathPrinter" LayoutTitle="InfoPathPrinter">
              <Section Alignment="Top" Type="OneRow">
                <Row>
                  <ControlRef DisplayMode="Large" TemplateAlias="PrintButton"/>
                </Row>
              </Section>
            </Layout>
          </GroupTemplate>
        </CommandUIDefinition>
        <CommandUIDefinition Location="Ribbon.Tabs.InfoPathListDisplayTab.Scaling._children">
          <MaxSize Id="WireBear.Ribbon.Groups.InfoPathPrinter.MaxSize"
                   Sequence="15"
                   GroupId="WireBear.Ribbon.Groups.InfoPathPrinter"
                   Size="InfoPathPrinter"/>
        </CommandUIDefinition>
        <CommandUIDefinition Location="Ribbon.Tabs.InfoPathListDisplayTab.Groups._children">
          <Group Id="WireBear.Ribbon.Groups.InfoPathPrinter"
                 Sequence="15"
                 Description="InfoPath Form Printing"
                 Title="Print"
                 Template="WireBear.Ribbon.Templates.Groups.InfoPathPrinter">
            <Controls Id="WireBear.Ribbon.Groups.InfoPathPrinter.Controls">
              <Button Id="WireBear.Ribbon.Groups.InfoPathPrinter.Print"
                  Command="WireBear.Commands.InfoPathPrinter.Print"
                  Sequence="5"
                  Image16by16="~/_layouts/images/InfoPathPrinter/InfoPathPrinterIcon16.png"
                  Image32by32="~/_layouts/images/InfoPathPrinter/InfoPathPrinterIcon.png"
                  Description="Prints an InfoPath Form"
                  LabelText="Print"
                  TemplateAlias="PrintButton"/>
            </Controls>
          </Group>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="WireBear.Commands.InfoPathPrinter.Print"
                          CommandAction="javascript:
                          var patt = /.+_FormControl.+__ViewContainer/gi;
                          var alldivs = document.getElementsByTagName('div');
                          var printpageHTML = '';
                          for(var i=0; i&lt;alldivs.length; i++){
                            if(patt.test(alldivs[i].id)){
                              printpageHTML = '&lt;HTML&gt;&lt;HEAD&gt;\n' +
                                              document.getElementsByTagName('HEAD')[0].innerHTML +
                                              '&lt;/HEAD&gt;\n&lt;BODY&gt;\n' + 
                                              alldivs[i].innerHTML.replace('inline-block','block') + 
                                              '\n&lt;/BODY&gt;&lt;/HTML&gt;';
                              break;
                            }
                          }
                          var printWindow = window.open('','printWindow');
                          printWindow.document.open();
                          printWindow.document.write(printpageHTML);
                          printWindow.document.close();
                          printWindow.print();
                          printWindow.close();
                          SP.UI.Notify.addNotification('Chris Kent is Awesome!');
                          ">
        </CommandUIHandler>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>
